<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Channel extends BaseController {

	public function __construct() {
        parent::__construct();
        // 加载数据库类
        $this->load->model('classesModel');
    }

    // 测试页面
    public function test($channelId = 101){
        $dataSource = '_sae/channel/create_channel/'. $channelId;
        $channelData = file_get_contents(web_url($dataSource));
        $channelData = json_decode($channelData, true);
        $this->socketUrl = $channelData['data'];

        $this->display(ADMIN_DIR. DIRECTORY_SEPARATOR. "channel". DIRECTORY_SEPARATOR. "test");
    }


    // 发送测试页面
    public function send_test($channelId = 101){
        $this->load->library('socket');
        var_dump($this->socket->send_sae_channel_msg($channelId, "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1555822124311&di=e51408d4bec06739be82a1f3e83850a1&imgtype=0&src=http%3A%2F%2Fimg.blog.163.com%2Fphoto%2Fuw5EwhYN7t82S1_U4-Cuaw%3D%3D%2F3388677244620087761.jpg"));
    }


    // 创建websocket连接
    public function create_channel($clientId){
        exit(json("", true, BASE_SOCKET_LINK));
        // exit(json("", true, 'ws://192.168.1.68:2048'));
        
    }

    // 客户端连接回调
    public function connected(){    
        $brandId = str_replace(CHANNEL_ID_PREFIX, "", $this->post('from'));
        // 获取班牌信息
        $brandInfo = $this->classesModel->get_brand_info($brandId);
        if(!$brandInfo) return;
        // 更新班牌在线状态
        $data['is_online'] = 1;
        $data['online_time'] = date("Y-m-d H:i:s");
        $this->db->update(ClassesModel::$brandTable, $data, "id='{$brandInfo['id']}'");
        // 写入日志记录
        file_get_contents("http://bw0663.com/test.php?school={$brandInfo['id']}&value=连接". json_encode($_POST));
    }

    // 客户端连接断开回调
    public function disconnected(){  
        $brandId = str_replace(CHANNEL_ID_PREFIX, "", $this->post('from'));
        // 获取班牌信息
        $brandInfo = $this->classesModel->get_brand_info($brandId);
        if(!$brandInfo) return;
        // 更新班牌在线状态
        $data['is_online'] = 0;
        $data['offline_time'] = date("Y-m-d H:i:s");
        $this->db->update(ClassesModel::$brandTable, $data, "id='{$brandInfo['id']}'");
        // 写入日志记录
        file_get_contents("http://bw0663.com/test.php?school={$brandInfo['id']}&value=断开". json_encode($_POST));
    }

    // 客户端收到消息回调
    public function message(){
        // 推送消息到客户端
        $this->load->library('socket');



        $brandId = str_replace(CHANNEL_ID_PREFIX, "", $this->post('from'));
        $message = $this->post("message");
        // 如果是心跳包，不查询数据库。减少数据库压力
        if($message == 1) return;

        // 获取班牌信息
        $brandInfo = $this->classesModel->get_brand_info($brandId);
        if(!$brandInfo) return;

        // 获取接收到的消息
        $data = json_decode($message, true);
        if(!isset($data['type'])) return;

        // socket数据
        $socketData['type'] = $data['type'];
        $socketData['msg'] = $data['msg'];

        // 如果是截屏, 就发送回客户端
        if($data['type'] == CONTENT_TYPE_SCREENSHOT) {
            $channelId = str_replace(CHANNEL_ID_PREFIX, "", $data['to']);
            $this->socket->send_sae_channel_msg($channelId, $socketData);
        }
        // 如果是获取版本信息, 就发送回客户端
        if($data['type'] == CONTENT_TYPE_GETVERSION) {
            $channelId = str_replace(CHANNEL_ID_PREFIX, "", $data['to']);
            $this->socket->send_sae_channel_msg($channelId, $socketData);
        }
        // 如果是获取考勤数据, 就发送回客户端
        if($data['type'] == CONTENT_TYPE_SIGN_DATA) {
            $channelId = str_replace(CHANNEL_ID_PREFIX, "", $data['to']);
            $this->socket->send_sae_channel_msg($channelId, $socketData);
        }
    }
}